import requests
import re
import json

# 请求URL
url = "https://sou.chinanews.com/search/news"

# 请求参数
data = {
    "q": "俄乌冲突",
    "searchField": "all",
    "sortType": "time",
    "dateType": "1year",
    "startDate": "2023-08-18",
    "endDate": "2024-08-18",
    "channel": "all",
    "editor": "",
    "shouQiFlag": "show",
    "pageNum": 1
}

# 请求头
headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "Accept-Encoding": "gzip, deflate, br, zstd",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "Cache-Control": "max-age=0",
    "Connection": "keep-alive",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "sou.chinanews.com",
    "Origin": "https://sou.chinanews.com",
    "Referer": "https://sou.chinanews.com/search/news",
    "Sec-Fetch-Dest": "document",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "same-origin",
    "Sec-Fetch-User": "?1",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0",
    "sec-ch-ua": '"Chromium";v="124", "Microsoft Edge";v="124", "Not-A.Brand";v="99"',
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": '"Windows"'
}

# 发送POST请求
response = requests.post(url, headers=headers, data=data)

# 解析响应内容
content = response.text

# 使用正则表达式提取"docArr"变量中的JSON数据
pattern = re.compile(r'var docArr = (\[.*?\]);')
match = pattern.search(content)

if match:
    docArr = match.group(1)
    # 将JSON字符串转换为Python对象
    articles = json.loads(docArr)
    # print(articles)
    # 提取文章信息
    for article in articles:
        title = article.get('title', 'N/A')
        pub_time = article.get('pubtime', 'N/A')
        content_without_tag = article.get('content_without_tag', 'N/A')
        url = article.get('url', 'N/A')
        print(f"标题: {title}")
        print(f"发布时间: {pub_time}")
        print(f"简介: {content_without_tag}")
        print(f"文章链接地址: {url}")
else:
    print("未能找到'docArr'数据。")
